package com.noreg.common;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import com.noreg.vo.Article;


/**
 * 提交文章类
 * @author jeson.sha
 * @blog http://www.ij2ee.com/
 */
public class CommonPostAction {
	XmlRpcClientConfigImpl config = null;
	XmlRpcClient client = null;
	
	public CommonPostAction(String xmlrpc) {
		if (config == null) {
			config = new XmlRpcClientConfigImpl();
			try {
				config.setServerURL(new URL(xmlrpc));
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (client == null)
				client = new XmlRpcClient();
			client.setConfig(config);
		}
		// TODO Auto-generated constructor stub
	}
	private static CommonPostAction action;
	
	public static CommonPostAction getInstance(String xmlrpc){
		if(action == null){
			action = new CommonPostAction(xmlrpc);
		}
		return action;
	}

	@SuppressWarnings("unchecked")
	public String postArticle(Article a) {

		Map post = new HashMap();
		post.put("title", a.getTitle());// 标题
		post.put("mt_keywords", a.getKeyword()); // CnBlogsParser.tag);// 标签
		/*
		 * Object[] categories = a.getCategories();//CnBlogsParser.tag };// 分类
		 * if(categories == null||categories.length==0){ categories = new
		 * Object[]{a.getKeyword()}; }
		 */
		Object[] categories = new Object[] { a.getKeyword() };
		post.put("categories", categories);
		post.put("description", a.getBody());// 内容
		Object[] params = new Object[] { a.isDisPost() ? "1" : "0",
				a.getUser(), a.getPwd(), post, true }; // 1代表正式发布，0代表草稿
		String ob;
		try {
			ob = (String) client.execute("metaWeblog.newPost", params);
		} catch (XmlRpcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				Thread.sleep(7000L);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return "";
		}

		System.out.println("Created with blogid " + ob);
		return "";
	}

	@SuppressWarnings("unchecked")
	public String postArticle(Article a, String keyword, Object[] categories,String user,String pwd) {
		Map post = new HashMap();
		post.put("title", a.getTitle());// 标题
		post.put("mt_keywords", a.getKeyword()); // CnBlogsParser.tag);// 标签
		post.put("categories", new Object[] { a.getKeyword() });

		post.put("description", a.getBody());// 内容
		Object[] params = new Object[] { a.isDisPost() ? "1" : "0", user,
				pwd, post, true }; // 1代表正式发布，0代表草稿
		String ob;
		try {
			ob = (String) client.execute("metaWeblog.newPost", params);
		} catch (XmlRpcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				Thread.sleep(7000L);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return "";
		}

		System.out.println("Created with blogid " + ob);
		return "";

	}
}
